package servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import service.CustomerManager;
import service.impl.CustomerManagerFactory;

/**
 * Servlet implementation class DeleteCustomerServlet
 */
public class DeleteCustomerServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private CustomerManager manager;

	public DeleteCustomerServlet() {
		super();
		manager = CustomerManagerFactory.getInstance();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
	
		Integer uid =  Integer.parseInt(request.getParameter("id"));
		if(this.manager.deleteCustomer(uid)==1){
			//成功刪除，刷新列表
			// 重定向
			try {
				response.sendRedirect("list");
			} catch (IOException e) {
				e.printStackTrace();
			}
		}else{
			///刪除用戶失敗，輸出提示
			request.setAttribute("msg", "update error！");
			try {
				request.getRequestDispatcher("/ErrorMsgServlet").forward(request, response);
			} catch (ServletException | IOException e) {
				e.printStackTrace();
			}
		}
	}

}
